package com.example.maruko_course_backend.utils;

import com.example.maruko_course_backend.criterias.PageCriteria;
import com.example.maruko_course_backend.dto.PageDto;
import com.example.maruko_course_backend.dto.SentenceDto;
import com.example.maruko_course_backend.entity.Sentence;
import org.springframework.data.domain.PageRequest;

import java.util.List;
import java.util.stream.Collectors;

public class PageUtil {
  public static PageRequest createPageRequest(PageCriteria pageCriteria) {
    return PageRequest.of(pageCriteria.getCurrentPage() - 1, pageCriteria.getPageSize());
  }

  public static PageDto<SentenceDto> createPageDtos(long totalSize, List<Sentence> pageRecords) {
    PageDto<SentenceDto> resultPage = new PageDto<>((int) totalSize);
    resultPage.setData(createSentenceDtosBySentences(pageRecords));
    return resultPage;
  }

  private static List<SentenceDto> createSentenceDtosBySentences(List<Sentence> content) {
    return content.stream().map(item -> SentenceDto.builder()
        .videoNum(item.getUpVideoNum())
        .sentenceSeq(item.getSentenceSeq())
        .sentenceContent(item.getSentenceOriginalStr())
        .sentenceChinese(item.getSentenceChinese())
        .build()
    ).collect(Collectors.toList());
  }
}
